<?php

namespace App\Manage;


use App\Models\SiteInfo;
use App\Service\Base\Manage;

class NewsCommentManage extends Manage
{

    // 基本配置
    public function initialize()
    {
        parent::initialize();

        $this->model = '';
    }

    // 列表
    // 添加
    // 修改

    // 软删除
    protected function toSoft()
    {
        $_id = $this->id();
        if (empty($_id)) {
            return $this->_returnFail('请指定删除记录');
        }

        $where = $this->delWhere();
        if ($where === false) {
            return $this->_returnFail($this->error);
        }

        $model  = $this->checkModel($this->model);
        $result = $model->isCache(true)->oneEdit($where, ['verify' => -1]);
        if ($result === false) {
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 列表查询
    protected function listWhere()
    {
        $option  = $this->option();
        $where   = [];
        $bang     = [];
        $news_id = $option->getData('news_id', 0, 'intval');
        $uid     = $option->getData('uid', 0, 'intval');
        $name    = $option->getData('name', null, 'trim');
        $keyword = $option->getData('keyword', null, 'trim');
        if ($news_id) {
            $bang[]  = 'news_id';
            $where[] = ['news_id', '=', $news_id];
        }
        if ($uid) {
            $bang[]  = 'uid';
            $where[] = ['uid', '=', $uid];
        }
        if ($name) {
            $bang    = false; // 设置不在缓存
            $where[] = ['name', 'like', "%" . $name . "%"];
        }
        if ($keyword) {
            $bang    = false; // 设置不在缓存
            //$where[] = $this->orWhere('username|nickname|mobile', 'like', $keyword);
            $where[] = ['content', 'like', "%" . $keyword . "%"];
        }

        // 缓存
        $this->bang = $bang;

        return $where;
    }

    // 添加之前
    protected function _before_add()
    {
        $opt  = $this->option();
        $data = [
            //'id'      => $opt->getData("id", null, "intval"),
            'news_id' => $opt->getData("news_id", null, "intval"),
            'uid'     => $opt->getData("uid", null, "intval"),
            'content' => $opt->getData("content", null, "trim"),
            'ip'      => $opt->getData("ip", null, "trim"),
            'verify'  => $opt->getData("verify", 1, "intval"),
            //'ctime'   => $opt->getData("ctime", null, "intval"),
            //'mtime'   => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }

    // 修改之前
    protected function _before_edit()
    {
        $opt  = $this->option();
        $data = [
            //'id'      => $opt->getData("id", null, "intval"),
            'news_id' => $opt->getData("news_id", null, "intval"),
            'uid'     => $opt->getData("uid", null, "intval"),
            'content' => $opt->getData("content", null, "trim"),
            'ip'      => $opt->getData("ip", null, "trim"),
            'verify'  => $opt->getData("verify", 1, "intval"),
            //'ctime'   => $opt->getData("ctime", null, "intval"),
            //'mtime'   => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }
}
